package exilim.exf1.cspacker;

import java.io.File;
import java.io.FileFilter;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.MetadataException;

import exilim.exf1.cspacker.util.FileUtil;

/**
 * 
 * @author Janos
 * @version 14.08.2010 | 17:02:54
 * 
 */
public class CSPackerLogic {

	private static final int EXIF_CASIO_TAGTYPE_SEQUENCE_NUMBER = 12545;
	private static final int EXIF_CASIO_TAGTYPE_SEQUENCE_ORDERING = 12546;

	public static File[] getFileList(final File sourceDirectory) {
		final File[] jpgFiles = sourceDirectory.listFiles(new FileFilter() {
			@Override
			public boolean accept(final File pathname) {
				return pathname.getName().toLowerCase().endsWith(".jpg");
			}
		});
		return jpgFiles;
	}

	public static void organizeSequenceFiles(final File dir, final Map<Integer, Map<Integer, File>> sequences) {
		for (final Entry<Integer, Map<Integer, File>> sequence : sequences.entrySet()) {
			final String folderName = createSequenceFolderName(sequence.getValue());

			final File seqFolder = new File(dir.getAbsolutePath() + File.separator + folderName);
			final boolean mkdir = seqFolder.mkdir();
			if (mkdir) {
				System.out.println("Directory for sequence created: " + seqFolder.getAbsolutePath());
			} else {
				System.out.println("Directory for sequence couldn't be created: " + seqFolder.getAbsolutePath());
			}

			for (final Entry<Integer, File> file : sequence.getValue().entrySet()) {
				final File destFile = new File(seqFolder + File.separator + file.getValue().getName());
				final boolean bMoved = file.getValue().renameTo(destFile);
				if (bMoved) {
					System.out.println("Moved file to sequence folder: " + file.getValue().getAbsolutePath());
				} else {
					System.out.println("Couldn't move file to sequence folder: " + file.getValue().getAbsolutePath());
				}
			}
		}
	}

	public static String createSequenceFolderName(final Map<Integer, File> sequence) {
		final File file = sequence.get(0);
		final String firstFile = FileUtil.getInstance().getName(file);
		return firstFile + "_" + sequence.size();
	}

	/**
	 * @param jpgFiles
	 */
	public static Map<Integer, Map<Integer, File>> getSequences(final File[] jpgFiles) {

		final Map<Integer, Map<Integer, File>> result = new HashMap<Integer, Map<Integer, File>>();

		for (final File file : jpgFiles) {
			final Metadata metadata;
			try {
				metadata = JpegMetadataReader.readMetadata(file);
			} catch (final JpegProcessingException e) {
				e.printStackTrace();
				continue;
			}

			final Directory directory = metadata.getDirectory(com.drew.metadata.exif.CasioType2MakernoteDirectory.class);
			if (directory == null) {
				// No Casio EXIF data available.
				continue;
			}

			try {
				final int serNumber = directory.getInt(EXIF_CASIO_TAGTYPE_SEQUENCE_NUMBER);
				if (serNumber == 0) {
					continue;
				}

				Map<Integer, File> sequenceFiles = result.get(serNumber);
				if (sequenceFiles == null) {
					sequenceFiles = new HashMap<Integer, File>();
					result.put(serNumber, sequenceFiles);
				}

				final int seqNumber = directory.getInt(EXIF_CASIO_TAGTYPE_SEQUENCE_ORDERING);
				sequenceFiles.put(seqNumber, file);
			} catch (final MetadataException e) {
				continue;
			}

		}

		return result;
	}

}
